package com.pstreets.lwuit.demo.widgets;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Log;
import com.sun.lwuit.util.Resources;

import javax.microedition.midlet.MIDlet;

/**
 * @author james
 */
public class ComboBoxDemo extends MIDlet {

    public void startApp() {

        //init the LWUIT display
        Display.init(this);

        //setting the application theme
        try {
            Resources r = Resources.open("/LWUITtheme.res");
            UIManager.getInstance()
                    .setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
        } catch (Exception e) {
        }

        Form mainForm = new Form("ComboBox Demo");
        mainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        String[] content = {"Red", "Blue", "Green", "Yellow"};
        // 1. Creating the combo box
        ComboBox comboBox = new ComboBox(content);
        // 2. Setting a checkBox renderer
        comboBox.setListCellRenderer(new checkBoxRenderer());
        // 3. Adding a action listener to catch user clicking
        // to open the ComboBox
        comboBox.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae) {
               Log.p(ae.toString());
            }
        });

        mainForm.addComponent(comboBox);
        mainForm.setTransitionOutAnimator(CommonTransitions.createFade(400));
        mainForm.addCommand(new Command("Left soft key", 2));
        mainForm.addCommand(new Command("Right Soft key", 2));
        mainForm.show();


    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    
    
    /**
 * Demonstrates implementation of a renderer derived from a
CheckBox
 */
private static class checkBoxRenderer extends CheckBox implements
        ListCellRenderer {

    //Creates a new instance of checkBoxRenderer
    public checkBoxRenderer() {
        super("");
    }
   // Setting the current check box text and status
    public Component getListCellRendererComponent(List list,
            Object value, int index, boolean isSelected) {
        setText("" + value);
        if (isSelected) {
            setFocus(true);
            setSelected(true);
        } else {
            setFocus(false);
            setSelected(false);
        }
        return this;
    }
// Returning the list focus component
    public Component getListFocusComponent(List list) {
        setText("");
        setFocus(true);
        setSelected(true);
        return this;
    }

      
    }
}

